package com.syqc.monitor.popup

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.utils.startActivityTo
import com.syqc.entity.SmsCmdInfo
import com.syqc.monitor.R
import com.syqc.monitor.ui.sms.SMSSendActivity
import com.syqc.utils.Keys

/**
 *@Author Jasper
 *@Time   2021/1/11
 *@Desc   短信指令弹窗
 */
class SMSCmdPopup(context: Context) : PopupWindow() {
    private var titleText: TextView? = null
    private val mAdapter by lazy { SMSPopupAdapter(context) }
    private var children: SmsCmdInfo.Children.Children? = null

    init {
        initView(context)
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        isClippingEnabled = false
    }


    private fun initView(context: Context) {
        contentView = LayoutInflater.from(context).inflate(R.layout.popup_sms, null)
        titleText = contentView.findViewById(R.id.tv_sms_popup_title)

        val recyclerSms = contentView.findViewById<RecyclerView>(R.id.recyclerView)
        recyclerSms.layoutManager = LinearLayoutManager(context)
        recyclerSms.adapter = mAdapter

        mAdapter.setOnItemClickListener { adapter, view, position ->
            children = mAdapter.data[position]
            mAdapter.index = position
            mAdapter.notifyDataSetChanged()
        }
        contentView.findViewById<TextView>(R.id.tvPositive).setOnClickListener {
            dismiss()
        }
        contentView.findViewById<TextView>(R.id.tvNegative).setOnClickListener {
            startActivityTo<SMSSendActivity>(context) {
                putExtra(Keys.INTENT_OTHER, children?.desc)
                putExtra(Keys.INTENT_TYPE, titleText?.text)
                putExtra(Keys.INTENT_DATA, children?.cmd)
            }
            dismiss()
        }

    }

    fun showAsDropDown(
        anchor: View,
        title: String,
        data: MutableList<SmsCmdInfo.Children.Children>
    ) {
        showAsDropDown(anchor)
        titleText?.text = title
        mAdapter.setList(data)
        mAdapter.index = 0
        children = data[0]
    }

    fun showAtLocation(
        anchor: View,
        title: String,
        data: MutableList<SmsCmdInfo.Children.Children>
    ) {
        showAtLocation(anchor, Gravity.NO_GRAVITY, 0, 0)
        titleText?.text = title
        mAdapter.setList(data)
        mAdapter.index = 0
        children = data[0]
    }

    inner class SMSPopupAdapter(mContext: Context) :
        BaseQuickAdapter<SmsCmdInfo.Children.Children, BaseViewHolder>(R.layout.item_popup_sms) {
        var index = -1
        override fun convert(
            holder: BaseViewHolder,
            item: SmsCmdInfo.Children.Children
        ) {
            item.run {
                val ivSms = holder.getView<ImageView>(R.id.ivSms)
                ivSms.isSelected = index == holder.layoutPosition
                holder.setText(R.id.tv_popup_sms_desc, desc)
                    .setText(R.id.tv_popup_sms_cmd, cmd)
            }
        }

    }
}